package com.asktgapp.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.asktgapp.R;


/**
 * time:2018/1/31
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

@SuppressLint("ValidFragment")
public class TokenInvalidDialog extends DialogFragment implements View.OnClickListener {
    private onResultCallBack onResultCallBack;
    private TextView mSure,mHint;
    private String mHintText="登录已失效，请重新登录";

    @SuppressLint("ValidFragment")
    public TokenInvalidDialog(String mHintText,onResultCallBack onResultCallBack) {
        this.mHintText = TextUtils.isEmpty(mHintText)?this.mHintText:mHintText;
        this.onResultCallBack = onResultCallBack;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getContext());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_token);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        mSure = dialog.findViewById(R.id.tv_sure);
        mHint = dialog.findViewById(R.id.mHint);
        mSure.setOnClickListener(this);
        mHint.setText(mHintText);
        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        WindowManager.LayoutParams params = window.getAttributes();
        WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        window.setBackgroundDrawableResource(R.drawable.dialog_bg);
        Display display = windowManager.getDefaultDisplay();
        params.width = display.getWidth() * 3 / 5;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        return dialog;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_sure:
                dismiss();
                onResultCallBack.onSure();

                break;
        }
    }


    public interface onResultCallBack {
        void onSure();
    }



}
